package com.sun.lotter.luck;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

import com.sun.base.BasePlayType;
import com.sun.bata.LotterData;
import com.sun.bata.MakeLotterNumber;
import com.sun.lotter.LotterNumberDialog;
import com.sun.lotter.R;

public class LuckPlayPage3 extends BasePlayType {
	Spinner spinner_lotter, spinner_play, spinner_year, spinner_month;
	Spinner spinner_day;
	static ArrayList<String> list_years = new ArrayList<String>();
	static ArrayList<String> list_days = new ArrayList<String>();
	ArrayAdapter<String> adapter_days;
	int nowYear = 0;

	public LuckPlayPage3(LuckLotterActivity context) {
		super(context);
		Calendar c = Calendar.getInstance();
		nowYear = c.get(Calendar.YEAR);
		list_years.clear();
		for (int i = 0; i < 150; i++) {
			list_years.add((nowYear - i) + "年");
		}
		LayoutInflater.from(context).inflate(R.layout.luck_layout_3, this);

		// //选择年份
		ArrayAdapter<String> adapter_year = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, list_years);
		adapter_year
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner_year = (Spinner) findViewById(R.id.Spinner_Year);
		spinner_year.setAdapter(adapter_year);
		spinner_year.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				GetDayCountFromYearMonth(
						(int) (nowYear - spinner_year.getSelectedItemId()),
						(int) spinner_month.getSelectedItemId() + 1);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		// /选择月份
		spinner_month = (Spinner) findViewById(R.id.Spinner_Month);
		spinner_month.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				GetDayCountFromYearMonth(
						(int) (nowYear - spinner_year.getSelectedItemId()),
						(int) spinner_month.getSelectedItemId() + 1);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		// //选择日期

		spinner_day = (Spinner) findViewById(R.id.Spinner_Day);
		adapter_days = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, list_days);
		adapter_days
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		GetDayCountFromYearMonth(
				(int) (c.get(Calendar.YEAR) - spinner_year.getSelectedItemId()),
				(int) spinner_month.getSelectedItemId() + 1);
		spinner_day.setAdapter(adapter_days);

		
		
		findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				MakeLotterNumber makeLotterNumber = new MakeLotterNumber();
				Random rand = makeLotterNumber.getRandom(new Date(System.currentTimeMillis()), 0, null, LotterData.DOUBLECOLORBALL, 0, 3);
				 ArrayList<String> list = makeLotterNumber.makeLotterNumbers(rand, LotterData.DOUBLECOLORBALL, 0, 1);
				 for (String string : list) {
					System.out.println(string);
				}
				 new LotterNumberDialog(getContext(), list, LotterData.DOUBLECOLORBALL, 0).show();
			}
		});
	}

	@Override
	public void onEvent(String e) {

	}

	/**
	 * 取得某年某月的最大天数
	 * 
	 * @param nYear
	 * @param nMonth
	 * @return
	 */
	private void GetDayCountFromYearMonth(int nYear, int nMonth) {

		Calendar c = Calendar.getInstance();
		/**
		 * 获得当前年份的最大天数
		 * */
		c.set(Calendar.YEAR, nYear);
		c.set(Calendar.MONTH, nMonth - 1);
		int maxdays = c.getActualMaximum(Calendar.DAY_OF_MONTH);

		if (maxdays == list_days.size()) {
			return;
		}
		if (spinner_day.getSelectedItemId() >= maxdays) {
			spinner_day.setSelection(0);
		}
		for (; list_days.size() < maxdays;) {
			list_days.add((list_days.size() + 1) + "日");
		}
		for (; list_days.size() > maxdays;) {
			list_days.remove(list_days.size() - 1);
		}
		adapter_days.notifyDataSetChanged();
	}

}
